<?php


namespace app\teacher\controller;


use app\teacher\utils\User as AdminUser;
use think\Db;

class ChangeRoom extends Base
{
    public function index()
    {
        $page = request()->param('page', '1', 'intval');
        $limit = request()->param('limit', 10, 'intval');
        $student_no = request()->param('student_no', '');
        $status = request()->param('status', '');
        $uid = AdminUser::getUid();
        $user_type = $this->user_type();
        // 教师所管的学生id
        if($user_type == 1){//辅导员
            $student_ids = model('Member')->where('fdy', $uid)->column('uid');
        }else{
            $xueyuan_id = Db::name('yuan')->where('yuanzhang',$uid)->value('id');
            $student_ids = model('Member')->where('xueyuan', $xueyuan_id)->column('uid');
        }
        $where[] = ['m.uid', 'in', $student_ids];
        if ($student_no) {
            $where[] = ['m.student_no', 'like', '%' . $student_no . '%'];
        }
        if ($status != '') {
            $where[] = ['cr.status', '=', $status];
        }else{
            if($user_type == 1){
                //教师处理状态status 0，1，2
                $where[] = ['cr.status','in','0,1,2'];
            }else{
                //院长处理状态 1=》老师同意 3=》院长同意 4=》院长拒绝
                $where[] = ['cr.status','in','1,3,4'];
            }
        }
        $list = Db::name('change_room')->alias('cr')
            ->join('member m', 'cr.uid = m.uid')
            ->where($where)->page($page, $limit)
            ->field('m.uid,m.username,m.student_no,m.zhuanye,cr.id,cr.house_id,cr.room_id,cr.reason,cr.create_time,cr.status,cr.teach_remark,cr.yuanzhang_remark')
            ->order('cr.id desc')
            ->select();
        foreach ($list as $k => $v) {
            $list[$k]['house_id'] = model('House')->where('id',$v['house_id'])->value('title');
            $list[$k]['room_id'] = model('Room')->where('id',$v['room_id'])->value('title');
            $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        }
        $total = Db::name('change_room')->alias('cr')
            ->join('member m', 'cr.uid = m.uid')
            ->where($where)->count();
        $data = [
            'list' => $list,
            'total' => $total
        ];
        return $this->ok($data);
    }

    public function detail(){
        $id = request()->param('id', 0, 'intval');
        if (!$id) {
            return $this->fail('参数错误');
        }
        $detail = Db::name('change_room')->find($id);
        if (empty($detail)) {
            return $this->fail('查无该学生换宿申请信息');
        }
        return $this->ok($detail);
    }

    public function save() {
        $params = request()->only(['teach_remark', 'id', 'uid','status','yuanzhang_remark']);
        if (!isset($params['id'])) {
            return $this->fail('参数错误');
        }
        $res = Db::name('change_room')->where(['id' => $params['id'], 'uid' => $params['uid']])->update($params);
        if ($res >= 0) {
            return $this->ok('修改成功');
        } else {
            return $this->fail('修改失败');
        }

    }
}